/**
* 系统管理  项目管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>项目管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="项目名称">
        <el-input v-model="formInline.serchData.projectName" placeholder="输入项目名称" size="small"></el-input>
      </el-form-item>
      <el-form-item label="请输入项目描述">
        <el-input v-model="formInline.serchData.description" placeholder="请输入项目描述" size="small"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="small" type="primary" @click="search">查询</el-button>
        <el-button icon="el-icon-plus" size="small" type="primary" @click="">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button icon="el-icon-search" size="small" type="primary" @click="handleEdit()">新建项目</el-button>

    <!--    TODO（新建项目单选框，不正确）-->
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="listData" highlight-current-row v-loading="loading"
              border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column label="ID" prop="id" sortable width="50">
      </el-table-column>
      <el-table-column label="项目名称" prop="projectName" sortable width="300">
      </el-table-column>
      <el-table-column label="APP类型" prop="platform" sortable width="300">
      </el-table-column>
      <el-table-column label="描述" prop="description" sortable width="300">
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" sortable width="300">
      </el-table-column>
      R
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="deleteProject(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 新增项目页面 -->
    <el-dialog :title="title" :visible.sync="addFormVisible" width="30%" @click="closeDialog">
      <el-form ref="addProjectForm" :model="addProjectForm" label-width="120px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="addProjectForm.projectName" auto-complete="off" placeholder="请输入权限名称"
                    size="small"></el-input>
        </el-form-item>
        <el-form-item label="APP类型" prop="platform">
          <el-radio-group v-model="radio">
            <el-radio :label="1">Android</el-radio>
            <el-radio :label="2">ios</el-radio>
            <el-radio :label="3">pc端</el-radio>
            <el-radio :label="4">微信小程序端</el-radio>
            <el-radio :label="5">H5链接</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="addProjectForm.description" auto-complete="off" placeholder="" size="small"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button :loading="loading" class="title" size="small" type="primary" @click="submitForm('addProjectForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {roleDropDown} from '../../api/userMG'
import Pagination from '../../components/Pagination'
import {addProject, listPage, removeProject} from "../../api/sysconfigMG";

export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      addFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      addProjectForm: {
        description: '',
        platform: 1,
        projectName: '',
        title: ''
      },
      //请求参数
      formInline: {
        pageNum: 1,
        pageSize: 10,
        pageCount: 10,
        serchData: {
          projectName: '',
          description: '',
        }
      },
      // 选择数据
      selectdata: [],
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        pageNum: 1,
        pageSize: 10,
        pageCount: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  watch: {},

  /**
   * 创建完毕
   */

  created() {
    this.getdata(this.formInline)
    this.getAccsee()
  },

  /**
   * 里面的方法只有被调用才会执行
   */

  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true

      /***
       * 调用接口，获取项目列表
       */
      listPage(parameter)
        .then(res => {
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.listData = res.result.list
            // 分页赋值
            this.pageparm.pageNum = this.formInline.pageNum
            this.pageparm.pageSize = this.formInline.pageSize
            this.pageparm.pageCount = res.pageCount
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('权限管理列表获取失败，请稍后再试！')
        })
    },
    // 获取权限
    getAccsee() {
      roleDropDown()
        .then(res => {
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.userparm = res.data
          }
        })
        .catch(err => {
          this.$message.error('权限获取失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.addFormVisible = true
      // 深拷贝并赋值
      //this.addProjectForm = Object.assign({}, row)
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.addProjectForm.description = row.description
        this.addProjectForm.projectName = row.projectName
        this.addProjectForm.description = row.description
      } else {
        this.title = '添加'
        this.addProjectForm.description = ''
        this.addProjectForm.projectName = ''
        this.addProjectForm.description = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          addProject(this.addProjectForm)
            .then(res => {
              this.addFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '项目新增成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.addFormVisible = false
              this.loading = false
              this.$message.error('项目新增失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除权限
    deleteProject(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          removeProject(row.description)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '权限管理已删除！'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('权限管理删除失败,请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.addFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>

